import { smc } from "../../../../common/ecs/SingletonModuleComp";
import { RoleAttributeType } from "../../../../role/model/RoleEnum";
import { Role } from "../../../../role/Role";
import { BattleResultType } from "../../../scene/model/BattleEnum";
import { BattleType } from "../../../scene/SceneEvent";

/** 角色状态验证 */
export class RoleState {
    /** 角色是否已死亡 */
    static isDie(role: Role): boolean {
        return role.getNumericValue(RoleAttributeType.hp_battle) == 0;
    }
    /** 是否冰冻状态 */
    static isFrozen(role: Role): boolean {
        return this.checkBuff(role, 2);
    }

    /** 是否眩晕状态 */
    static isDizzy(role: Role): boolean {
        return this.checkBuff(role, 3);
    }

    /** 是否沉默状态 */
    static isSilent(role: Role): boolean {
        return this.checkBuff(role, 4);
    }

    /** 是否灼烧状态 */
    static isFire(role: Role): boolean {
        return this.checkBuff(role, 7);
    }

    /** 是否中毒状态 */
    static isPoison(role: Role): boolean {
        return this.checkBuff(role, 6);
    }

    /** 是否流血状态 */
    static isBlood(role: Role): boolean {
        return this.checkBuff(role, 1);
    }

    /**
     * 检查战斗结果
     */
    static checkBattleResult(battleType: BattleType) {
        var bm!: any;
        var mmr!: any;
        switch (battleType) {
            case BattleType.Scene:
                bm = smc.scene.BattleModel;
                mmr = smc.scene.MapModelRole;
                break;
            case BattleType.Boss:
                // bm = smc.boss.BossBattleModel;
                // mmr = smc.boss.BossMapModelRole;
                break;
            case BattleType.Tower:
                // bm = smc.tower.TowerBattleModel;
                // mmr = smc.tower.TowerMapModelRole;
                break;
            default:
                break;
        }
        if(bm.battleResultType != BattleResultType.Battling){
            return;
        }

        let friendsNum = 0;
        let enemysNum = 0;
        let type: BattleResultType = BattleResultType.Battling;

        (mmr.enemys as Role[]).forEach(element => {
            if (element.getNumericValue(RoleAttributeType.hp_battle) > 0) {
                enemysNum += 1;
            }
        });

        (mmr.friends as Role[]).forEach(element => {
            if (element.getNumericValue(RoleAttributeType.hp_battle) > 0) {
                friendsNum += 1;
            }
        });

        if(mmr.friends.length){
            if (friendsNum && enemysNum) {
                type = BattleResultType.Battling;
            }
            else if (friendsNum) {
                type = BattleResultType.Win;
                this.removeAllDecorators(battleType);
            }
            else {
                type = BattleResultType.Lose;
                this.removeAllDecorators(battleType);
            }
        }else{
            if (enemysNum) {
                type = BattleResultType.Battling;
            }else {
                type = BattleResultType.Win;
            }
        }

        bm.battleResultType = type;
    }
    
    /**
     * 战斗结束删除武将的装饰器属性，释放怪物
     */
    static removeAllDecorators(battleType: BattleType) {
        var mmr!: any;
        switch (battleType) {
            case BattleType.Scene:
                mmr = smc.scene.MapModelRole;
                break;
            case BattleType.Boss:
              //  mmr = smc.boss.BossMapModelRole;
                break;
            case BattleType.Tower:
              //  mmr = smc.tower.TowerMapModelRole;
                break;
        
            default:
                break;
        }
        (mmr.friends as Role[]).forEach(role => {
            role.removeAllDecorators();
        });

        (mmr.enemys as Role[]).forEach(enemy => {
            enemy.removeAllDecorators();
            // enemy.destroy();
        });
    }

    /**
    * 检查是否有某个id的buff
    * @param role 
    * @param id 表配置的buff id
    */
    private static checkBuff(role: Role, id: number): boolean {
        var hasBuff = false;
        // role.RoleModelBattle.buffs.forEach(element => {
        //     if (element.BuffModel.table.id == id) {
        //         hasBuff = true;
        //     }
        // });

        return hasBuff;
    }

}